<?php
namespace Home\Controller;
use Think\Controller;
class MyController extends Controller {
	public $scBook;
    public $Book;
    public function __construct() {
        parent::__construct();
        $this->scBook = M('shoucang');
        $this->Book = M('addbook');
    } 
    public function my(){
        
     if($_SESSION['openid']){
        $date['openid'] = $_SESSION['openid'];
        $weixin = M('user')->where($date)->count();
        if($weixin == 0){
           M('user')->openid = $_SESSION['openid'];
           M('user')->headimgurl = $_SESSION['headimgurl'];
           M('user')->yonghuname = $_SESSION['yonghuname'];
           M('user')->tel = '微信用户' ;
            $arr = M('user')->add(); 
            $user=M('user');
            $data['openid'] = $_SESSION['openid'];
            $id=$user->where($data)->field('id,yonghuname')->find();
            $_SESSION['id']=$id['id'];
            $_SESSION['yonghuname'] = $id['yonghuname'];      
            $date['openid'] = $_SESSION['openid'];
            $usermoney=$user->where($date)->field('yue')->find();//用户所有钱数
            $this->assign('money', $usermoney['yue']);
            $this->display();
        } else {
            $user=M('user');
            $data['openid'] = $_SESSION['openid'];
            $id=$user->where($data)->field('id,yonghuname')->find();
            $_SESSION['id']=$id['id']; 
            $_SESSION['yonghuname'] = $id['yonghuname'];           
            $date['openid'] = $_SESSION['openid'];
            $usermoney=$user->where($date)->field('yue')->find();//用户所有钱数
            $this->assign('money', $usermoney['yue']);
            $this->display();   
        }
}elseif($_SESSION['id']){
             
             $da['aid'] = $_SESSION['id'];
             $person=M('person');
             $imgurl=$person->where($da)->field('imgurl')->find();
             $_SESSION['imgurl']=$imgurl;
             
             $data['id'] = $_SESSION['id'];
             $usermoney=M('user')->where($data)->field('yue')->find();//
             $this->assign('money', $usermoney['yue']);           
             $this->display();

} else {
    $this->display();
}


       


           
       // if($_SESSION['headimgurl']){
       //      $user=M('user');
       //      $data['headimgurl'] = $_SESSION['headimgurl'];
       //      $id=$user->where($data)->field('id,yonghuname')->find();
       //      $_SESSION['id']=$id['id'];           
       //      $date['headimgurl'] = $_SESSION['headimgurl'];
       //      $usermoney=$user->where($date)->field('yue')->find();//用户所有钱数
       //      $this->assign('money', $usermoney['yue']);
       //      $this->display();
       //  }else{
       //      $data['id'] = $_SESSION['id'];
       //      $da['aid'] = $_SESSION['id'];
       //      $person=M('person');
       //      $imgurl=$person->where($da)->field('imgurl')->find();
       //      $_SESSION['imgurl']=$imgurl;
       //      $usermoney=M('user')->where($data)->field('yue')->find();//
       //      $this->assign('money', $usermoney['yue']);           
       //      $this->display();
       //  }
        
        
    }

    public function sy(){

        $this->display();
    }
    public function out(){
    	if($_SESSION['yonghuname']){
           session('yonghuname',null);
           session('[destroy]');
           $this->success('退出成功！', U('My/my'));
        } else {
            $this->error("您还未登录！", U('My/my'));
        }
        // $this->redirect('My/my');
    }
    public function person(){

    	if($_SESSION['yonghuname']){

    		$this->display();
    	}else{
    		$this->error("您还未登录！", U('My/my'));
    	}
    }
   
    public function upload(){
    	$yonghuname=$_SESSION['yonghuname'];
    	$user=M('user');
    	$person=M('person');
    	$id=$user->where("yonghuname='$yonghuname'")->field('id')->select();
    	$aid=$id[0]['id'];
    	$ii=$person->where("aid=$aid")->select();

    	$upload=new \Think\Upload();
        $upload->exts= array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  = './Public/Uploads/images/'; // 设置附件上传根目录
        $info = $upload -> upload();
            
        if (!$info){  
            $this->error($upload->getError());  
        }else{  
               
            foreach($info as $file){
            $file_path="./Public/Uploads/images/".$file['savepath'].$file['savename'];
            $image = new \Think\Image();
            $image->open($file_path);
            $image->thumb(120, 120)->save($file_path);

            }
               
        } 
        $imgurl=$file_path;
    	$name=$_POST['personname'];
    	$sex=$_POST['personsex'];
    	$birthday=$_POST['personbirth'];
    	$job=$_POST['personjob'];
    	$place=$_POST['personclass'];
    	$xuehao=$_POST['personhao'];
    	$qinshihao=$_POST['personplace'];
    	$ss=$_POST['personii'];
    	$phone=$_POST['personphone'];
    	$mail=$_POST['personmail'];
    	$sign=$_POST['personsign'];
    	$da=array('name'=>$name,'sex'=>$sex,'birthday'=>$birthday,'job'=>$job,'place'=>$place,'ss'=>$ss,'phone'=>$phone,'mail'=>$mail,'sign'=>$sign,'imgurl'=>$imgurl,'xuehao'=>$xuehao,'qinshihao'=>$qinshihao,'aid'=>$aid);
    	
    	if($ii){
    		$value=$person->where("aid=$aid")->save($da);

    	}else{
    		$value=$person->add($da);
    		
    	}
    	
    	if($value){
    		$aid=$_SESSION['id'];
    		$imgurl=$person->where("aid=$aid")->field('imgurl')->select();
    		$_SESSION['imgurl']=$imgurl[0]['imgurl'];

    		$this->success('保存成功！', U('My/my'));
    	}else{
    		$this->error("未保存成功，请重新添加！", U('My/person'));
    	}
    	
    }
    public function jieyue(){
    	
        if($_SESSION['yonghuname']){

            $jieshu=M('jieshu');
            $addbook=M('addbook');
            $userid=$_SESSION['id'];
            $data['userid'] = $userid;
            $data['zhuangtai'] = 1;
            $value=$jieshu->where($data)->field('bookid')->select();
            if(!$value){
                
                $this->display();
                exit;
            }

            foreach ($value as $k => $v) {
                $ff[]=$v["bookid"];

            }
        
            $data['id'] = array('in',$ff);
            $vs=$addbook->where($data)->select();
            $this->assign('Book',$vs);
       
  
            $this->display();
        }else{
            $this->error("您还未登录！", U('My/my'));
        }
    }
    public function shoucang(){
    	$userid = $_SESSION['id'];
        if (!$_SESSION['id']) {
        	$this->assign('inf','你的收藏空空的,赶快登陆查看吧');
            $this->display();
        	exit();
        } else {
        $userid = $_SESSION['id'];
        $data['userid'] =  $userid ;
        $bookid = $this->scBook->field('bookid')->where($data)->select();
        }
        if ($bookid) {
         	 $ids = array();
         foreach ($bookid as $k => $v) {   //把二维数组转化为一维数组
             $ids[] = $v['bookid'];
         }
         $map['id'] = array('in',$ids);
        $scbook = $this->Book->field('id,title,suotu,writer,note')->where($map)->select();
        $this->assign('scbook',$scbook);
          
        } else {
        	$this->assign('inf','你还没收藏书籍,赶快去收藏吧');
        }

    	$this->display();
    }
    public function comment(){
        $comment=M('comment');
        $name=$_SESSION['yonghuname'];
        $value=$comment->where("username='$name'")->order("id desc")->select();
        $this->assign('tuijianBook',$value);
        $this->display();
    }
}